/**
 * ************************************************
 * Filename: RobTicket.java Version: v1.0 CreatedDate: 2011-11-27 Copyright (C)
 * 2011 By cafebabe.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see http://www.gnu.org/licenses/.
 *
 * If you would like to negotiate alternate licensing terms, you may do so by
 * contacting the author: talentyao@foxmail.com
 * *************************************************
 */
package com.ywh.train;

import com.ywh.train.Config;
import com.ywh.train.Constants;
import com.ywh.train.ResourceManager;
import com.ywh.train.Util;
import my12306.bean.PassengerInfo;
import my12306.TrainClient;
import my12306.http.HttpClientUtils;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;
import org.apache.http.client.HttpClient;

/**
 * 订票机器人
 *
 * @author cafebabe
 * @since 2011-11-27
 * @version 1.0
 */
public class RobTicket {

    private JFrame frame;
//	private JLabel lblRc;
    private JTextField txtUsername;
    private JTextField txtPassword;
//	private JTextField txtRandCode;
    private JTextField txtUserID;
    private JTextField txtName;
    private JTextField txtPhone;
    private JTextField txtStartDate;
    private JTextField txtFromStation;
    private JTextField txtToStation;
    private JCheckBox boxkIsAuto;
    private JComboBox boxoRang;
    private JTextArea textArea;
    private JButton btnSORE;
    private JButton btnOpenIE;
    private HttpClient httpClient = null;
    private TrainClient trainClient = null;
    private LogicThread logic;
    PassengerInfo ui = new PassengerInfo();
    private JTextField txtTrainNo;
    private DefaultListModel dlm;
    private JCheckBox boxkLockTrain;
    private JCheckBox boxkOneSeat;
    private JCheckBox boxkTwoSeat;
    private JCheckBox boxkHardSleeper;
    private JCheckBox boxkHardSeat;
    private JCheckBox boxkSoftSleeper;
    private JCheckBox boxkSoftSeat;
    private JCheckBox boxkBussSeat;
    private JCheckBox boxkBestSeat;
    private JCheckBox boxkNoneSeat;
    private JCheckBox boxkOther;
    private JCheckBox boxkStrinStation;
    private JComboBox boxoCardType;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        log("main thread started.");
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager
                            .getSystemLookAndFeelClassName());
                    RobTicket window = new RobTicket();
                    window.frame.setVisible(true);
                    log("started.");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        log("main thread ended.");
    }

    public static void log(String msg) {
        System.out.println(msg);
    }

    /**
     * Create the application.
     */
    public RobTicket() {
//        initNetwork();
        this.httpClient = HttpClientUtils.getHttpsClient();
        this.trainClient = new TrainClient(this.httpClient);
//        installCert();
//        initProxy();
        initialize();
        Runtime.getRuntime().addShutdownHook(new ExitThread());
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame(ResourceManager.getString("RobTicket.frameName")); //$NON-NLS-1$
        ImageIcon ico = ResourceManager.createImageIcon("logo.jpg"); //$NON-NLS-1$
        frame.setIconImage(ico.getImage());
        frame.setBounds(100, 100, 524, 516);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        ToolTipManager.sharedInstance().setInitialDelay(0);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10, 22, 498, 54);
        frame.getContentPane().add(panel);
        panel.setLayout(null);
        panel.setBorder(new TitledBorder(ResourceManager
                .getString("RobTicket.panelBorderName"))); //$NON-NLS-1$ //$NON-NLS-2$

        JLabel label = new JLabel(ResourceManager.getString("RobTicket.txtUsername")); //$NON-NLS-1$
        label.setBounds(32, 26, 43, 15);
        panel.add(label);
        label.setHorizontalAlignment(SwingConstants.RIGHT);

        txtUsername = new JTextField(Config.getAccount());
        txtUsername.setToolTipText(ResourceManager
                .getString("RobTicket.txtUsernameTip")); //$NON-NLS-1$
        txtUsername.setBounds(79, 23, 91, 21);
        panel.add(txtUsername);
        txtUsername.setColumns(10);

        JLabel label_1 = new JLabel(
                ResourceManager.getString("RobTicket.txtPassword")); //$NON-NLS-1$
        label_1.setBounds(205, 26, 43, 15);
        panel.add(label_1);
        label_1.setHorizontalAlignment(SwingConstants.RIGHT);

        txtPassword = new JPasswordField(Config.getPassword());
        txtPassword.setToolTipText(ResourceManager
                .getString("RobTicket.txtPasswordTip")); //$NON-NLS-1$
        txtPassword.setBounds(260, 23, 84, 21);
        panel.add(txtPassword);
        txtPassword.setColumns(10);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(10, 81, 498, 143);
        panel_1.setBorder(new TitledBorder(
                UIManager.getBorder("TitledBorder.border"), ResourceManager.getString("RobTicket.panel_1BorderTitle"), TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); //$NON-NLS-1$ //$NON-NLS-2$
        frame.getContentPane().add(panel_1);
        panel_1.setLayout(null);

        JLabel label_3 = new JLabel(ResourceManager.getString("RobTicket.txtUserID")); //$NON-NLS-1$
        label_3.setBounds(179, 29, 73, 15);
        panel_1.add(label_3);
        label_3.setHorizontalAlignment(SwingConstants.RIGHT);

        txtUserID = new JTextField();
        txtUserID
                .setToolTipText(ResourceManager.getString("RobTicket.txtUserIDTip")); //$NON-NLS-1$
        txtUserID.setBounds(262, 26, 127, 21);
        panel_1.add(txtUserID);
        txtUserID.setColumns(10);

//        JLabel label_5 = new JLabel(ResourceManager.getString("RobTicket.txtName")); //$NON-NLS-1$
        JLabel label_5 = new JLabel("姓 名:"); //$NON-NLS-1$
        label_5.setBounds(216, 56, 36, 15);
        panel_1.add(label_5);
        label_5.setHorizontalAlignment(SwingConstants.RIGHT);

        txtName = new JTextField();
//        txtName.setToolTipText(ResourceManager.getString("RobTicket.txtNameTip")); //$NON-NLS-1$
        txtName.setToolTipText("乘车人姓名"); //$NON-NLS-1$
        txtName.setBounds(262, 53, 84, 21);
        panel_1.add(txtName);
        txtName.setColumns(10);

        JLabel label_6 = new JLabel(
                ResourceManager.getString("RobTicket.txtStartDate")); //$NON-NLS-1$
        label_6.setBounds(193, 84, 61, 15);
        panel_1.add(label_6);
        label_6.setHorizontalAlignment(SwingConstants.RIGHT);

        MaskFormatter mf = null;
        try {
            mf = new MaskFormatter("####-##-##"); //$NON-NLS-1$
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        txtStartDate = new JFormattedTextField(mf);
        txtStartDate.setToolTipText(ResourceManager
                .getString("RobTicket.txtStartDateTip")); //$NON-NLS-1$
        txtStartDate.setBounds(262, 81, 84, 21);
        panel_1.add(txtStartDate);
        txtStartDate.setColumns(10);
        txtStartDate.setText(Config.getStartDate());

        JLabel label_4 = new JLabel(
                ResourceManager.getString("RobTicket.txtFromStation")); //$NON-NLS-1$
        label_4.setBounds(32, 109, 43, 15);
        panel_1.add(label_4);
        label_4.setHorizontalAlignment(SwingConstants.RIGHT);

        txtFromStation = new JTextField();
        txtFromStation.setToolTipText(ResourceManager
                .getString("RobTicket.txtFromStationTip")); //$NON-NLS-1$
        txtFromStation.setBounds(79, 106, 92, 21);
        panel_1.add(txtFromStation);
        txtFromStation.setColumns(10);
        txtFromStation.setText(Config.getFromStation());

        JLabel label_7 = new JLabel(
                ResourceManager.getString("RobTicket.txtToStation")); //$NON-NLS-1$
        label_7.setBounds(218, 109, 36, 15);
        panel_1.add(label_7);
        label_7.setHorizontalAlignment(SwingConstants.RIGHT);

        txtToStation = new JTextField();
        txtToStation.setToolTipText(ResourceManager
                .getString("RobTicket.txtToStationTip")); //$NON-NLS-1$
        txtToStation.setBounds(262, 106, 84, 21);
        panel_1.add(txtToStation);
        txtToStation.setColumns(10);
        txtToStation.setText(Config.getToStation());

        JLabel label_8 = new JLabel(ResourceManager.getString("RobTicket.txtPhone")); //$NON-NLS-1$
        label_8.setBounds(10, 54, 61, 15);
        panel_1.add(label_8);
        label_8.setHorizontalAlignment(SwingConstants.RIGHT);
        try {
            mf = new MaskFormatter("###########"); //$NON-NLS-1$
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        txtPhone = new JFormattedTextField(mf);
        txtPhone.setToolTipText(ResourceManager.getString("RobTicket.txtPhoneTip")); //$NON-NLS-1$
        txtPhone.setBounds(79, 53, 92, 21);
        panel_1.add(txtPhone);
        txtPhone.setColumns(10);

        JLabel label_9 = new JLabel(ResourceManager.getString("RobTicket.boxoRang")); //$NON-NLS-1$
        label_9.setBounds(0, 84, 73, 15);
        panel_1.add(label_9);
        label_9.setHorizontalAlignment(SwingConstants.RIGHT);
        boxoRang = new JComboBox(Constants.getRangTime());
        boxoRang.setToolTipText(ResourceManager.getString("RobTicket.boxoRangTip")); //$NON-NLS-1$
        boxoRang.setBounds(79, 81, 92, 21);
        boxoRang.setSelectedItem(Config.getRangTime());
        panel_1.add(boxoRang);

        JButton button = new JButton(ResourceManager.getString("RobTicket.addBtn")); //$NON-NLS-1$
        button.addActionListener(new AddAction());
        button.setToolTipText(ResourceManager.getString("RobTicket.addBtnTip")); //$NON-NLS-1$
        button.setBounds(415, 52, 73, 23);
        panel_1.add(button);

        boxkStrinStation = new JCheckBox(ResourceManager.getString("RobTicket.boxkStrinStation")); //$NON-NLS-1$
        boxkStrinStation.setToolTipText(ResourceManager.getString("RobTicket.boxkStrinStationTip")); //$NON-NLS-1$
        boxkStrinStation.setBounds(350, 105, 73, 23);
        panel_1.add(boxkStrinStation);

        JLabel label_2 = new JLabel(ResourceManager.getString("RobTicket.label_2")); //$NON-NLS-1$
        label_2.setHorizontalAlignment(SwingConstants.RIGHT);
        label_2.setBounds(0, 29, 73, 15);
        panel_1.add(label_2);

        boxoCardType = new JComboBox(Constants.getCardType());
        boxoCardType.setToolTipText(ResourceManager.getString("RobTicket.comboBoxTip")); //$NON-NLS-1$
        boxoCardType.setBounds(79, 26, 92, 21);
        panel_1.add(boxoCardType);

        boxkIsAuto = new JCheckBox(ResourceManager.getString("RobTicket.boxkIsAuto")); //$NON-NLS-1$
        boxkIsAuto.setBounds(390, 230, 109, 23);
        boxkIsAuto.setToolTipText(ResourceManager
                .getString("RobTicket.boxkIsAutoTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkIsAuto);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(146, 316, 362, 162);
        scrollPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED, null,
                null, null, null));
        scrollPane
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        frame.getContentPane().add(scrollPane);

        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
        textArea.setText(ResourceManager.getString("RobTicket.textAreaContent")); //$NON-NLS-1$
        textArea.setEditable(false);
        textArea.setLineWrap(true);

        btnSORE = new JButton(ResourceManager.getString("RobTicket.btnSORE")); //$NON-NLS-1$
        btnSORE.setBounds(330, 270, 60, 23);
        btnSORE.setToolTipText(ResourceManager.getString("RobTicket.btnSORETip")); //$NON-NLS-1$
        frame.getContentPane().add(btnSORE);

        btnOpenIE = new JButton(ResourceManager.getString("RobTicket.btnOpenIE"));
        btnOpenIE.setBounds(400, 270, 110, 23);
        btnOpenIE.setToolTipText(ResourceManager.getString("RobTicket.btnOpenIE.tip"));
        frame.getContentPane().add(btnOpenIE);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 518, 21);
        frame.getContentPane().add(menuBar);

        JMenu mnOpt = new JMenu(ResourceManager.getString("RobTicket.mnOpt")); //$NON-NLS-1$
        menuBar.add(mnOpt);

        JMenuItem miOpt = new JMenuItem(ResourceManager.getString("RobTicket.miOpt")); //$NON-NLS-1$
        // mnOpt.add(miOpt);
        miOpt.addActionListener(new UseSkillAction(frame));

        JMenuItem miExit = new JMenuItem(
                ResourceManager.getString("RobTicket.miExit")); //$NON-NLS-1$
        mnOpt.add(miExit);
        miExit.addActionListener(new ExitAction());

        JMenu mnHelp = new JMenu(ResourceManager.getString("RobTicket.mnHelp")); //$NON-NLS-1$
        menuBar.add(mnHelp);

        JMenuItem miAbout = new JMenuItem(
                ResourceManager.getString("RobTicket.miAbout")); //$NON-NLS-1$
        miAbout.addActionListener(new AboutAction(frame));
        mnHelp.add(miAbout);

        txtTrainNo = new JTextField();
        txtTrainNo.setBounds(87, 231, 201, 21);
        txtTrainNo.setToolTipText(ResourceManager.getString("RobTicket.txtTrainNoTip")); //$NON-NLS-1$

        txtTrainNo.setColumns(10);
        frame.getContentPane().add(txtTrainNo);

        JLabel label_10 = new JLabel(ResourceManager.getString("RobTicket.label_10")); //$NON-NLS-1$
        label_10.setBounds(20, 234, 63, 15);
        label_10.setHorizontalAlignment(SwingConstants.RIGHT);
        frame.getContentPane().add(label_10);

        boxkLockTrain = new JCheckBox(ResourceManager.getString("RobTicket.boxkLockTrain")); //$NON-NLS-1$
        boxkLockTrain.setBounds(294, 230, 73, 23);
        boxkLockTrain.setToolTipText(ResourceManager.getString("RobTicket.boxkLockTrainTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkLockTrain);

        boxkOneSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkOneSeat")); //$NON-NLS-1$
        boxkOneSeat.setBounds(30, 258, 63, 23);
        boxkOneSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkOneSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkOneSeat);

        boxkTwoSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkTwoSeat")); //$NON-NLS-1$
        boxkTwoSeat.setSelected(true);
        boxkTwoSeat.setBounds(30, 283, 63, 23);
        boxkTwoSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkTwoSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkTwoSeat);

        boxkHardSleeper = new JCheckBox(ResourceManager.getString("RobTicket.boxkHardSleeper")); //$NON-NLS-1$
        boxkHardSleeper.setSelected(true);
        boxkHardSleeper.setBounds(95, 283, 49, 23);
        boxkHardSleeper.setToolTipText(ResourceManager.getString("RobTicket.boxkHardSleeperTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkHardSleeper);

        boxkHardSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkHardSeat")); //$NON-NLS-1$
        boxkHardSeat.setSelected(true);
        boxkHardSeat.setBounds(95, 258, 49, 23);
        boxkHardSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkHardSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkHardSeat);

        boxkSoftSleeper = new JCheckBox(ResourceManager.getString("RobTicket.boxkSoftSleeper")); //$NON-NLS-1$
        boxkSoftSleeper.setBounds(146, 258, 49, 23);
        boxkSoftSleeper.setToolTipText(ResourceManager.getString("RobTicket.boxkSoftSleeperTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkSoftSleeper);

        boxkSoftSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkSoftSeat")); //$NON-NLS-1$
        boxkSoftSeat.setBounds(146, 283, 49, 23);
        boxkSoftSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkSoftSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkSoftSeat);

        boxkBussSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkBussSeat")); //$NON-NLS-1$
        boxkBussSeat.setBounds(197, 258, 63, 23);
        boxkBussSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkBussSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkBussSeat);

        boxkBestSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkBestSeat")); //$NON-NLS-1$
        boxkBestSeat.setBounds(197, 283, 63, 23);
        boxkBestSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkBestSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkBestSeat);

        boxkNoneSeat = new JCheckBox(ResourceManager.getString("RobTicket.boxkNoneSeat")); //$NON-NLS-1$
        boxkNoneSeat.setBounds(262, 258, 49, 23);
        boxkNoneSeat.setToolTipText(ResourceManager.getString("RobTicket.boxkNoneSeatTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkNoneSeat);

        boxkOther = new JCheckBox(ResourceManager.getString("RobTicket.boxkOther")); //$NON-NLS-1$
        boxkOther.setBounds(262, 283, 49, 23);
        boxkOther.setToolTipText(ResourceManager.getString("RobTicket.boxkOtherTip")); //$NON-NLS-1$
        frame.getContentPane().add(boxkOther);

        dlm = new DefaultListModel();
        final JList list = new JList(dlm);
        list.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        list.setCellRenderer(new RobTicket.UserInfoCellRenderer());
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = list.getSelectedIndex();
                    if (index >= 0) {
                        dlm.remove(index);
                    }
                }
            }
        });
        list.setToolTipText(ResourceManager.getString("RobTicket.userListTip")); //$NON-NLS-1$
        list.setBounds(10, 342, 130, 136);

        frame.getContentPane().add(list);

        JLabel label_11 = new JLabel(ResourceManager.getString("RobTicket.label_11.text")); //$NON-NLS-1$
        label_11.setBounds(20, 323, 73, 15);
        frame.getContentPane().add(label_11);

//		refreshRandCode();
        readUserInfo();
        btnSORE.addActionListener(new StartAction());
        btnOpenIE.addActionListener(new OpenIeAction());

        String[] ids = Config.getUserInfoIDs();
        String[] names = Config.getUserInfoNames();
        String[] phones = Config.getUserInfoPhones();
        String[] cardTypes = Config.getUserInfoCardTypes();
        if (null == ids || null == names) {
            return;
        }

        int len = ids.length;
        for (int i = 0; i < len; i++) {
            this.txtUserID.setText(ids[i]);
            this.txtName.setText(names[i]);
            if (phones != null && phones.length > i) {
                this.txtPhone.setText(phones[i]);
            }
            if (cardTypes != null && cardTypes.length > i) {
                this.boxoCardType.setSelectedItem(cardTypes[i]);
            }
            button.doClick();
        }
    }

    /**
     * 复位操作
     *
     * @param isEnd 是否是结束复位,false为普通复位
     */
    public void reset(boolean isEnd) {
        if (isEnd) {
            btnSORE.setText(ResourceManager.getString("RobTicket.btnSORE")); //$NON-NLS-1$
            if (logic != null) {
//                logic.setEnd(true);
                logic.setDone();
            }
        } else {
            try {
                Thread.sleep(Config.getSleepTime());
            } catch (InterruptedException ex) {
            }
        }
    }

    /**
     * 启动方法
     */
    protected void action() {
        if (getUsername().isEmpty() || getPassword().isEmpty() || getFromCity().isEmpty()
                || getFromCity().isEmpty() || getToCity().isEmpty()
                || getStartDate().isEmpty()) {
            JOptionPane.showMessageDialog(frame,
                    ResourceManager.getString("RobTicket.JOptionPane")); //$NON-NLS-1$
            reset(true);
            return;
        }

        btnSORE.setText(ResourceManager.getString("RobTicket.btnSORE.end")); //$NON-NLS-1$
        textArea.setText(""); //$NON-NLS-1$

        if (dlm.getSize() == 0) {
            JOptionPane.showMessageDialog(frame, ResourceManager.getString("RobTicket.showMessageDialog")); //$NON-NLS-1$
            reset(true);
            return;
        }

        logic = new LogicThread(trainClient, this);
        logic.start();
    }

    /**
     * 获取乘车人姓名
     *
     * @return
     */
    private String getPassengerName() {
        return txtName.getText().trim();
    }

    /**
     * 获取乘车人ID
     *
     * @return
     */
    private String getPassengerID() {
        return txtUserID.getText().trim();
    }

    private PassengerInfo getPassengerInfo() {
        PassengerInfo pi = new PassengerInfo();
        pi.setIDType(getCardType());
        pi.setID(getPassengerID());
        pi.setName(getPassengerName());
        pi.setPhone(getPhone());
        pi.setFromStation(getFromCity());
        pi.setToStation(getToCity());
        pi.setStartDate(txtStartDate.getText());
        return pi;
    }

    /**
     * 获取乘车人电话
     *
     * @return
     */
    private String getPhone() {
        return txtPhone.getText().trim();
    }

    /**
     * 获取乘车时间段
     *
     * @return
     */
    public String getRangDate() {
        String key = (String) boxoRang.getSelectedItem();
        String rangDate = Constants.getTrainRang(key);
        return rangDate;
    }

    /**
     * 获取证件类型
     *
     * @return
     */
    public String getCardType() {
        String key = (String) boxoCardType.getSelectedItem();
        String cardType = Constants.cardType.get(key);
        return cardType;
    }

    /**
     * 获取乘车日期
     *
     * @return
     */
    public String getStartDate() {
        return txtStartDate.getText().trim();
    }

    /**
     * 获取出发站
     *
     * @return
     */
    public String getToCity() {
        return txtToStation.getText().trim();
    }

    /**
     * 获取目的站
     *
     * @return
     */
    public String getFromCity() {
        return txtFromStation.getText().trim();
    }

    /**
     * 获取登录密码
     *
     * @return
     */
    public String getPassword() {
        return txtPassword.getText().trim();
    }

    /**
     * 获取登录用户名
     *
     * @return
     */
    public String getUsername() {
        return txtUsername.getText().trim();
    }

    /**
     * 是否自动识别验证码
     *
     * @return
     */
    public boolean isAutocode() {
        return boxkIsAuto.isSelected();
    }

    /**
     * 获取乘车人信息
     */
    public List<PassengerInfo> getPassengers() {
        List<PassengerInfo> passengers = new ArrayList<PassengerInfo>();
        for (int i = 0; i < dlm.size(); i++) {
            passengers.add((PassengerInfo) dlm.getElementAt(i));
        }
        return passengers;
    }

    /**
     * 读取列车设置信息
     *
     * @return
     */
    public boolean[] getTrainSet() {
        boolean trainSet[] = new boolean[]{false, false, false, false, false,
            false, false, false, false, false, false, false};
        trainSet[Constants.isLockTrain] = boxkLockTrain.isSelected();
        trainSet[Constants.isStrinStation] = boxkStrinStation.isSelected();
        trainSet[Constants.isNeed_BEST_SEAT] = boxkBestSeat.isSelected();
        trainSet[Constants.isNeed_BUSS_SEAT] = boxkBussSeat.isSelected();
        trainSet[Constants.isNeed_HARD_SEAT] = boxkHardSeat.isSelected();
        trainSet[Constants.isNeed_HARD_SLEEPER] = boxkHardSleeper.isSelected();
        trainSet[Constants.isNeed_NONE_SEAT] = boxkNoneSeat.isSelected();
        trainSet[Constants.isNeed_ONE_SEAT] = boxkOneSeat.isSelected();
        trainSet[Constants.isNeed_VAG_SLEEPER] = boxkOther.isSelected();
        trainSet[Constants.isNeed_SOFT_SEAT] = boxkSoftSeat.isSelected();
        trainSet[Constants.isNeed_SOFT_SLEEPER] = boxkSoftSleeper.isSelected();
        trainSet[Constants.isNeed_TWO_SEAT] = boxkTwoSeat.isSelected();
        return trainSet;
    }

    /**
     * 获取指定列车信息
     *
     * @return
     */
    public Set<String> getTrainNo() {
        Set<String> ans = new HashSet<String>();
        String trainStr = txtTrainNo.getText().trim();
        String[] trainNo = trainStr.split("\\|"); //$NON-NLS-1$
        for (String str : trainNo) {
            if (!str.trim().isEmpty()) {
                ans.add(str.trim());
            }
        }
        return ans;
    }

    /**
     * 保存用户信息
     *
     * @param ui
     */
    private void savePassengerInfo(PassengerInfo ui) {
        ObjectOutputStream oos = null;
        try {
            File file = new File("ui");
            System.out.println("Filepath: " + file.getAbsolutePath());
            oos = new ObjectOutputStream(new FileOutputStream(file)); //$NON-NLS-1$
            oos.writeObject(ui);
            oos.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (Exception ex) {
//				e.printStackTrace();
            }
        }
    }

    /**
     * 读取用户信息
     */
    private void readUserInfo() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File("ui"))); //$NON-NLS-1$
            PassengerInfo ui = (PassengerInfo) ois.readObject();
            if (ui != null) {
                txtUserID.setText(ui.getID());
                txtName.setText(ui.getName());
                txtPhone.setText(ui.getPhone());
                txtFromStation.setText(ui.getFromStation());
                txtToStation.setText(ui.getToStation());
                txtStartDate.setText(ui.getStartDate());
            }
        } catch (Exception ex) {
//			e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ex) {
//				e.printStackTrace();
            }
        }
    }

    public void console(String content) {
        textArea.append(Util.formatInfo(content));
        textArea.setCaretPosition(textArea.getText().length());
    }

    public JFrame getFrame() {
        return frame;
    }

    /**
     * 功能描述
     *
     * @author cafebabe
     * @since 2012-1-11
     * @version 1.0
     */
    private final class AddAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (getPassengerID().isEmpty() || getPassengerName().isEmpty()) {
                JOptionPane.showMessageDialog(frame, ResourceManager.getString("RobTicket.addNone")); //$NON-NLS-1$
            } else if (dlm.getSize() >= 5) {
                JOptionPane.showMessageDialog(frame, ResourceManager.getString("RobTicket.addFive")); //$NON-NLS-1$
            } else {
                PassengerInfo user = new PassengerInfo();
                user.setID(getPassengerID());
                user.setName(getPassengerName());
                user.setPhone(getPhone());
                user.setIDType(getCardType());
                dlm.addElement(user);
//                txtName.setText(""); //$NON-NLS-1$
//                txtUserID.setText(""); //$NON-NLS-1$
//				System.out.println(user);
            }
        }
    }

    /**
     * 功能描述
     *
     * @author YAOWENHAO
     * @since 2011-12-21
     * @version 1.0
     */
    class ExitAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            savePassengerInfo(getPassengerInfo());
            System.exit(0);
        }
    }

    /**
     * 开始按钮的监听
     *
     * @author YAOWENHAO
     * @since 2011-12-21
     * @version 1.0
     */
    class StartAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            JButton btn = (JButton) e.getSource();
            if (ResourceManager.getString("RobTicket.btnSORE").equals(btn.getText())) {
                log("start button clicked.");
                action();
            } else {
                reset(true);
            }
        }
    }

    /**
     * 打开浏览器
     *
     * @author belerweb
     */
    class OpenIeAction implements ActionListener {

        Process ieProcess = null;

        public void actionPerformed(ActionEvent event) {
//            if (TrainClient.JSESSIONID == null || TrainClient.BIGipServerotsweb == null) {
//                JOptionPane.showMessageDialog(frame, ResourceManager.getString("RobTicket.btnOpenIE.wait"));
//                return;
//            }

            JButton btn = (JButton) event.getSource();
            if (ResourceManager.getString("RobTicket.btnOpenIE").equals(btn.getText())) {
                btn.setText(ResourceManager.getString("RobTicket.btnOpenIE.close"));
//                try {
//                    RegistryKey ieSettings = Registry.HKEY_CURRENT_USER.openSubKey(
//                            "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", RegistryKey.ACCESS_ALL);
//                    RegistryValue proxyEnable = new RegDWordValue(ieSettings, "ProxyEnable", RegistryValue.REG_DWORD, 1);
//                    RegistryValue proxyServer = new RegStringValue(ieSettings, "ProxyServer", "https=127.0.0.1:9999");
//                    ieSettings.setValue(proxyEnable);
//                    ieSettings.setValue(proxyServer);
//                    ieSettings.flushKey();
//                    ieProcess = Runtime.getRuntime().exec("explorer \"" + Constants.QUERY_ORDER_URL + "\"");
//                } catch (Exception ex) {
//                    ex.printStackTrace();
//                }
            } else {
                if (ieProcess != null) {
                    ieProcess.destroy();
                    ieProcess = null;
                }
//                try {
//                    RegistryKey ieSettings = Registry.HKEY_CURRENT_USER.openSubKey(
//                            "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", RegistryKey.ACCESS_ALL);
//                    RegistryValue proxyEnable = new RegDWordValue(ieSettings, "ProxyEnable", RegistryValue.REG_DWORD, 0);
//                    ieSettings.setValue(proxyEnable);
//                    ieSettings.flushKey();
//                } catch (Exception ex) {
//                    ex.printStackTrace();
//                }
                btn.setText(ResourceManager.getString("RobTicket.btnOpenIE"));
            }
        }
    }

    /**
     * 退出线程
     *
     * @author YAOWENHAO
     * @since 2011-12-21
     * @version 1.0
     */
    protected class ExitThread extends Thread {

        public void run() {
            httpClient.getConnectionManager().shutdown();
            savePassengerInfo(getPassengerInfo());
            System.out.println("exited.");
        }
    }

    /**
     *
     * 用户信息渲染类
     *
     * @author jhezjkp
     * @since 2012-1-12
     * @version 1.0
     */
    static class UserInfoCellRenderer extends JLabel implements
            ListCellRenderer {

        /**
         * 字段注释
         */
        private static final long serialVersionUID = -5357035693572356253L;

        public UserInfoCellRenderer() {
            setOpaque(true);
            setIconTextGap(12);
        }

        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            PassengerInfo userInfo = (PassengerInfo) value;
            setText(userInfo.getName() + " " + userInfo.getID()); //$NON-NLS-1$
            if (isSelected) {
                setBackground(Color.BLUE);
                setForeground(Color.WHITE);
            } else {
                setBackground(Color.WHITE);
                setForeground(Color.BLACK);
            }
            return this;
        }
    }

    /**
     * 菜单关于窗口
     *
     * @author YAOWENHAO
     * @since 2011-12-21
     * @version 1.0
     */
    static class AboutAction extends AbstractAction {

        /**
         * 字段注释
         */
        private static final long serialVersionUID = -1097396738396411124L;
        JFrame parentsFrame;
        URL img = ResourceManager.getFileURL("logo.jpg"); //$NON-NLS-1$
        String imagesrc = "<img src=" + img + " width=\"50\" height=\"50\">"; //$NON-NLS-1$ //$NON-NLS-2$
        String message = Constants.ABOUNT_CONTENT;

        protected AboutAction(JFrame frame) {
            this.parentsFrame = frame;
        }

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(
                    parentsFrame,
                    "<html><center>" + imagesrc + "</center><br><center>" + message + "</center><br></html>", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    ResourceManager.getString("RobTicket.AboutAction.title"), //$NON-NLS-1$
                    JOptionPane.DEFAULT_OPTION);


        }
    }

    /**
     * 使用技巧
     *
     * @author YAOWENHAO
     * @since 2011-12-21
     * @version 1.0
     */
    static class UseSkillAction extends AbstractAction {

        /**
         * 字段注释
         */
        private static final long serialVersionUID = -7920608352042404436L;
        JFrame parentsFrame;
        URL img = ResourceManager.getFileURL("logo.jpg"); //$NON-NLS-1$
        String imagesrc = "<img src=\"" + img + "\" width=\"50\" height=\"50\">"; //$NON-NLS-1$ //$NON-NLS-2$
        String message = ResourceManager
                .getString("RobTicket.UseSkillAction.message"); //$NON-NLS-1$

        protected UseSkillAction(JFrame frame) {
            this.parentsFrame = frame;
        }

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(
                    parentsFrame,
                    "<html>" + imagesrc + "<br><center>" + message + "</center><br></html>", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    ResourceManager
                    .getString("RobTicket.UseSkillAction.title"), //$NON-NLS-1$
                    JOptionPane.DEFAULT_OPTION);

        }
    }
}
